import { compileTemplate } from './compile.js'

const domNodePool = []; // 存放组件生成的dom节点

export function createApp (options) {

  for (let option in options) {
    switch (option) {
      case 'components':
        initComponent(options[option]);
        break;
      default:
        break;
    }
  }

  return {
    mount
  }
}

function initComponent(components) { // 初始化视图
  for(let component of components) {
    let [ template, state ] = component();
    // console.log(template); 组件的模板，编译成dom
    const node = compileTemplate(template, state);
    // console.log(node);
    domNodePool.push(node);
  }
  // console.log(domNodePool);
}

function mount(el) { // 挂载到dom上
  const app = document.querySelector(el);
  console.log(app);
  const oFrag = document.createDocumentFragment();
  domNodePool.forEach(node => {
    oFrag.appendChild(node);
  })
  app.appendChild(oFrag);
}
